package com.shilin.watchsystem.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/watchsystem/file")
public class FileUploadController {
    @Value("${web.upload-path}")
    String filePath;
    @Value("${web.ip}")
    String ip;

    @PostMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file) {
        Map<String,Object> map = new HashMap<>();
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        String fileName = UUID.randomUUID().toString()+".jpg";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            map.put("code",0);
            map.put("msg","成功");
            Map<String,Object> map1 = new HashMap<>();
            map1.put("src",ip+fileName);
            map.put("data",map1);
            return map;
        } catch (IOException e) {
        }
        return "上传失败！";
    }

}
